//
//  CompereViewModel.m
//  zhuchiren
//
//  Created by rainday on 15/1/30.
//  Copyright (c) 2015年 rainday. All rights reserved.
//

#import "CompereViewModel.h"
#import <AFNetworking.h>
#import <RACEXTScope.h>
#import <ReactiveCocoa.h>
#import "IP.h"
@interface CompereViewModel(){

    NSString *_url;
    
}

@property (nonatomic ,assign) NSInteger numOfPages;


@end
@implementation CompereViewModel


+ (id)shareCompereViewModel{
    
    static id compereViewModel;
    static dispatch_once_t once;
    dispatch_once(&once, ^{
        
        compereViewModel = [[CompereViewModel alloc] init];
    });
    
    return compereViewModel;
    
}

- (id)init{
    if (self = [super init]) {
        _dataArray = [NSMutableArray array];
        _numOfPages=1;
        _reloadData = NO;
        _url = [NSString stringWithFormat:@"%s/comperes",SERVER];
        
    }
    return self;
}

- (void)getCompereAtPage:(NSInteger)currentPage{
    
    
    if (currentPage>_numOfPages) {
        
        [[NSNotificationCenter defaultCenter] postNotificationName:@"allload" object:nil];
        
        return;
    }
    
    NSDictionary *params = @{@"current_page":[NSNumber numberWithInteger:currentPage],
                             @"num_per_page":[NSNumber numberWithInteger:10]};
    
    
    
    
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeNone];
    manager.requestSerializer = [AFHTTPRequestSerializer serializer];
    
    [manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Accept"];
    [manager.requestSerializer setValue:@"application/json; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
    __weak CompereViewModel *weakSelf = self;
    
    
    [manager GET:_url parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        [weakSelf addDataOfpage:currentPage data:responseObject];
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Error: %@", error);
        
    }];
    

}


- (RACSignal *)addCompere:(NSString *)compere phone:(NSString *)phone{
    
    @weakify(self);
    return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
        
        @strongify(self);
        NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
        id token = [userDefaults objectForKey:@"token"];
        
        
        NSDictionary *params = @{@"phone":phone,
                                 @"content":compere};
        
        
        
        AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
        manager.securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeNone];
        manager.requestSerializer = [AFHTTPRequestSerializer serializer];
        
        [manager.requestSerializer setValue:[NSString stringWithFormat:@"Token %@",token] forHTTPHeaderField:@"Authorization"];
        [manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Accept"];
        [manager.requestSerializer setValue:@"application/json; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
       
        
        
        [manager POST:_url parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
            [subscriber sendNext:responseObject];
            
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            [subscriber sendError:error];
            
        }];
        
        return nil;
    }];
    
   

}


- (NSInteger)numberOfTableView{

    
    return [_dataArray count];
}


- (void)addDataOfpage:(NSInteger)page data:(id)data{
    if (page==1) {
        [_dataArray removeAllObjects];
        _numOfPages = [(NSNumber *)[data objectForKey:@"num_pages"]  intValue];
    }
    
    
    [_dataArray addObjectsFromArray:[data objectForKey:@"list"]];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"reload" object:nil];

}

- (NSDictionary *)dictionaryAtIndexPath:(NSIndexPath *)indexPath{
    NSInteger row = indexPath.row;

    if (row < [_dataArray count]) {
        return (NSDictionary *)_dataArray[row];
        
    }
    return nil;

}
@end
